<?php
namespace app\exam\controller;
use app\exam\Model\User;
use think\Loader;
use think\Db;
use think\Cache;

class Practice extends Base
{

    /*
     * 1.获取是单选还是多选，综合，1是单选，2是综合，3多选
     * 2.查询当前用户是不是当前的类型的会员
     * 3.当前类型id
     * @param $question_type 当前题目的选择类型，单选还是多选
     * @param $login_flag  用户客户端的flag和
     *
     * */
    public function  myPractice(){
            $data=input();
            //Cache::rm($data['login_flag'].'_lianxizongheti'.$data['type_id']);die;
            $question_type=$data['question_type'];
            if($question_type==1){
                $res=Db::table('questiones')->where(['question_type'=>0,'comprehensive_id'=>0,'type_num'=>$data['type_id']])->select();
                if($res){
                    foreach ($res as $k1=>&$v) {
                        $array=[];
                        $str='';
                        if($v['has_img']==1){
                           $ret=Db::table('questionimg')->where(['question_id'=>$v['question_id']])->select();
                           foreach ($ret as $k => $v1) {
                              $str.=$v1['img_url'].',';
                           }
                           
                          $v['img']=$str;

                        }
                      $array[]=$res;
                  
                    }
                }
                  $cache=Cache::get($data['login_flag'].'_lianxidanxuanti'.$data['type_id']);
                 if($cache){
                    $key=$cache['key'];
                    $error=$cache['num'];
                    $question_id=$cache['question_id'];
                  }else{
                    $key=0;
                     $error=0;
                    $question_id='';
                  }
                $cache1=Cache::get($data['login_flag'].'_lianxi_danxuan_yesandno'.$data['type_id']);
                 if($cache1){
                     $next_id=$cache1['q_id'];
                     $status=$cache1['status'];
                 }else{
                     $next_id='';
                     $status='';
                 }

                $res=$array;

            }

            if($question_type==2){
                $res=Db::table('questiones')->where(['question_type'=>3,'comprehensive_id'=>0,'type_num'=>$data['type_id']])->select();
                foreach ($res as $k1=>&$v) {
                    $array=[];
                    $str='';
                    if($v['has_img']==1){
                       $ret=Db::table('questionimg')->where(['question_id'=>$v['question_id']])->select();
                       foreach ($ret as $k => $v1) {
                          $str.=$v1['img_url'].',';
                       }
                       
                      $v['img']=$str;

                    }
                  $array[]=$res;
                  
                }
                $cache=Cache::get($data['login_flag'].'_lianxiduoxuanti'.$data['type_id']);
                 if($cache){
                    $key=$cache['key'];
                     $error=$cache['num'];
                    $question_id=$cache['question_id'];
                  }else{
                    $key=0;
                     $error=0;
                    $question_id='';
                  }
                $cache1=Cache::get($data['login_flag'].'_lianxi_duoxuan_yesandno'.$data['type_id']);
                if($cache1){
                    $next_id=$cache1['q_id'];
                    $status=$cache1['status'];
                }else{
                    $next_id='';
                    $status='';
                }


                $res=$array;
                // print_r($cache);
            }

            if($question_type==3){
                $ret=Db::table('descquestion')->where(['type_num'=>$data['type_id']])->select();
                $array=[];
                foreach ($ret as $k => $v) {
                     $rept= Db::table('questiones')->where(['comprehensive_id'=>$v['c_id']])->select();
                       foreach ($rept as &$v2) {
                          
                            $str3='';
                            if($v2['has_img']==1){
                               $retp=Db::table('questionimg')->where(['question_id'=>$v2['question_id']])->select();
                               foreach ($retp as $k => $v1) {
                                  $str3.=$v1['img_url'].',';
                               }
                           
                            $v2['img']=$str3;
                            }
                           
                            $v2['desc']=$v['c_decs'];
                            $v2['c_id']=$v['c_id'];

                        }

                     $array[]=$rept;
                }
                $cache=Cache::get($data['login_flag'].'_lianxizongheti'.$data['type_id']);
                  if($cache){
                    $key=$cache['key'];
                      $error=$cache['num'];
                    $question_id=$cache['question_id'];
                  }else{
                    $key=0;
                      $error=0;
                    $question_id='';
                  }
                $cache1=Cache::get($data['login_flag'].'_lianxi_zonghe_yesandno'.$data['type_id']);
                if($cache1){
                    $next_id=$cache1['q_id'];
                    $status=$cache1['status'];
                }else{
                    $next_id='';
                    $status='';
                }
              
                $res=$array;
            }

            if($res){
                return json_encode(['code'=>10000,'msg'=>'查询成功','data'=>$res,'keywords'=>$key,'error_num'=>$error,'question_id'=>$question_id,'current_id'=>$next_id,'status'=>$status]);
            }else{
                return json_encode(['code'=>10001,'msg'=>'查询失败或暂无数据']);
            }
           


    }



    public function answer(){
        $data=input();
         //Cache::rm($data['login_flag'].'_lianxizongheti'.$data['type_id']);die;
        if($data){
            if($data['type']==1){
                         $array=[
                            'num'=>$data['num'],
                            'question_id'=>$data['question_id'],
                            'key'=>$data['keywords']
                        ];
                $rept=Cache::set($data['login_flag'].'_lianxidanxuanti'.$data['type_id'],$array);
 
                $cache=Cache::get($data['login_flag'].'_lianxidanxuanti'.$data['type_id']);
                // print_r($cache);die;
                return self::three($rept,'回答成功','回答失败');

            }


            if($data['type']==2){
                  $array=[
                            'num'=>$data['num'],
                            'question_id'=>$data['question_id'],
                            'key'=>$data['keywords']
                        ];
                $rept=Cache::set($data['login_flag'].'_lianxiduoxuanti'.$data['type_id'],$array);
 
                $cache=Cache::get($data['login_flag'].'_lianxiduoxuanti'.$data['type_id']);
                // print_r($cache);die;
                return self::three($rept,'回答成功','回答失败');
            }
           
           if($data['type']==3){
                $array=[
                            'num'=>$data['num'],
                            'question_id'=>$data['question_id'],
                            'key'=>$data['keywords']
                        ];
                $rept=Cache::set($data['login_flag'].'_lianxizongheti'.$data['type_id'],$array);
 
                $cache=Cache::get($data['login_flag'].'_lianxizongheti'.$data['type_id']);
                // print_r($cache);die;
                return self::three($rept,'回答成功','回答失败');
           }



        }
    }


    public function Whether(){
        $data=input();
        if($data){
            if($data['type']==1){
                    $array=[
                      'q_id'=>$data['q_id'],
                      'status'=>$data['status']
                    ];
                $rept=Cache::set($data['login_flag'].'_lianxi_danxuan_yesandno'.$data['type_id'],$array);
                return self::three($rept,'记录成功','记录失败');
            }
            if($data['type']==2){
                $array=[
                    'q_id'=>$data['q_id'],
                    'status'=>$data['status']
                ];
                $rept=Cache::set($data['login_flag'].'_lianxi_duoxuan_yesandno'.$data['type_id'],$array);
                return self::three($rept,'记录成功','记录失败');
            }
            if($data['type']==3){
                $array=[
                    'q_id'=>$data['q_id'],
                    'status'=>$data['status']
                ];
                $rept=Cache::set($data['login_flag'].'_lianxi_zonghe_yesandno'.$data['type_id'],$array);
                return self::three($rept,'记录成功','记录失败');
            }
        }else{
            return json(['code'=>10001,'msg'=>'参数错误']);
        }
    }

    public function count(){
        $data=input();
        $numone=Db::table('questiones')->where(['question_type'=>0,'comprehensive_id'=>0,'type_num'=>$data['type_id']])->count();
        $numtwo=Db::table('questiones')->where(['question_type'=>3,'comprehensive_id'=>0,'type_num'=>$data['type_id']])->count();
        $numthree=Db::table('descquestion')->where(['type_num'=>$data['type_id']])->count();
        $danxuan=Cache::get($data['login_flag'].'_lianxidanxuanti'.$data['type_id']);
        $duoxuan=Cache::get($data['login_flag'].'_lianxiduoxuanti'.$data['type_id']);
        $zonghe=Cache::get($data['login_flag'].'_lianxizongheti'.$data['type_id']);

        if($danxuan){
            $danxuan['key']=$danxuan['key']-1;
            $numone=[
                'wrong'=>$danxuan['num'],
                'correct'=>($danxuan['key']-$danxuan['num'])/$danxuan['key'],
                'keywords'=>$danxuan['key'],
                'numsum'=>$numone
            ];
        }else{
            $numone=[
                'wrong'=>0,
                'correct'=>0,
                'keywords'=>0,
                'numsum'=>$numone
            ];
        }


        if($duoxuan){
            $duoxuan['key']=$duoxuan['key']-1;
            $numtwo=[
                'wrong'=>$duoxuan['num'],
                'correct'=>($duoxuan['key']-$duoxuan['num'])/$duoxuan['key'],
                'keywords'=>$duoxuan['key'],
                'numsum'=>$numtwo
            ];
        }else{
            $numtwo=[
                'wrong'=>0,
                'correct'=>0,
                'keywords'=>0,
                'numsum'=>$numtwo
            ];
        }

        if($zonghe){
            $zonghe['key']=$zonghe['key']-1;
            $numthree=[
                'wrong'=>$zonghe['num'],
                'correct'=>($zonghe['key']-$zonghe['num'])/$zonghe['key'],
                'keywords'=>$zonghe['key'],
                'numsum'=>$numthree
            ];
        }else{
            $numthree=[
                'wrong'=>0,
                'correct'=>0,
                'keywords'=>0,
                'numsum'=>$numthree
            ];
        }
//        print_r($numtwo);
//        print_r($numone);
//        print_r($numthree);
//        print_r($numtwo);
      return json_encode(['code'=>10000,'msg'=>'成功','danxuan'=>$numone,'duoxuan'=>$numtwo,'zonghe'=>$numthree]);
    }
    



}

